package core

import (
	"fmt"
	"reflect"
)

type Err struct {
	Code int64
	Info string
	Post func()
}

func (err *Err) Error() string {
	return fmt.Sprintf("%s(%d)", err.Info, err.Code)
}

func ErrNew(info string) Err {
	return Err{
		Code: 0,
		Info: info,
		Post: nil}
}

func ErrHandle() {
	r := recover()
	switch r.(type) {
	case nil:
		Log.Debug("finish")
	case Err:
		err := r.(Err)
		if err.Post != nil {
			err.Post()
		} else {
			Log.Error(err.Error())
		}
	case error:
		Log.Error((r.(error)).Error())
		panic(r)
	default:
		Log.Error("Other error type:%#v", reflect.TypeOf(r).String())
		panic(r)
	}
}

func ChkErr(err error) {
	if err != nil {
		panic(ErrNew(err.Error()))
	}
}
